package com.jtoon.servlet;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jtoon.bean.Comics;
import com.jtoon.bean.Episode;
import com.jtoon.util.PMF;

public class EpisodeServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		int page = 1;
		int perPage = 10;
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query comicsListQry = pm.newQuery(Comics.class);
		Query totalQuery = pm.newQuery(Episode.class);
		Query pageQuery = pm.newQuery(Episode.class);
		long cid = Long.valueOf(request.getParameter("cid"));
		
		HttpSession session = request.getSession();
		if(session.getAttribute("id") != null){
			request.setAttribute("isLogin", true);
		}
		
		try{
			Comics comics = pm.getObjectById(Comics.class, cid);
			String comicsFolder = comics.getTitleImg().replace("img/", "").replace(".jpg", "");
			request.setAttribute("title", comics.getTitle());
			request.setAttribute("comicsFolder", comicsFolder);
			
			if(request.getParameter("page") != null){
				page = Integer.valueOf(request.getParameter("page"));
			} 
			 
			request.setAttribute("page", page);
			
			int start = ((page-1)*10)+1;
			int end = start + 9;
			 
			totalQuery.setFilter("cid == cidParam");
			totalQuery.declareParameters("int cidParam");
			totalQuery.setOrdering("order desc");
			List<Episode> episodeList = (List<Episode>)totalQuery.execute(cid); 
			int total = episodeList.size();
			
			if(total < end){
				end = total;
			} 
			 
			int totalPage = (total/perPage) + 1;
			if(total%perPage == 0){
				totalPage = totalPage - 1;
			}
			  
			request.setAttribute("total", total);
			request.setAttribute("totalPage", totalPage);
			
			pageQuery.setFilter("cid == cidParam");
			pageQuery.declareParameters("int cidParam");
			pageQuery.setOrdering("order desc");
			pageQuery.setRange(start-1, end);
			List<Episode> pageList = (List<Episode>)pageQuery.execute(cid);
			  
			request.setAttribute("start", start);
			request.setAttribute("end", end);
			request.setAttribute("pageList", pageList); 
			request.setAttribute("firstEpisode", episodeList.get(episodeList.size()-1));
			request.setAttribute("cid", cid);
			RequestDispatcher rd = request.getRequestDispatcher("/page/episode.jsp"); 
			rd.forward(request, response);  
		}finally{
			totalQuery.closeAll(); 
			pageQuery.closeAll();
			pm.close(); 
		}
		
	}
}
